
/*
 * @FilePath: 05着色器变量uniform.js
 * @Author: 杜芬
 * @Date: 2024-04-22 17:18:41
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-04-23 11:15:04
 * Copyright: 2024 xxxTech CO.,LTD. All Rights Reserved.
 * @Descripttion: 页面
 */
import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
let scene, camera, renderer, controls

function init() {
  scene = new THREE.Scene()
  camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
  camera.position.set(0, 0, 5)
  renderer = new THREE.WebGLRenderer({ antialias: true })
  renderer.setSize(window.innerWidth, window.innerHeight)
  document.body.appendChild(renderer.domElement)

  controls = new OrbitControls(camera, renderer.domElement)
  controls.enableDamping = true

  const axesHelper = new THREE.AxesHelper(5)
  scene.add(axesHelper)
};
function renderLoop() {
  renderer.render(scene, camera)
  controls.update()
  requestAnimationFrame(renderLoop)
}

 
// 正方形
function createBasic() {
 
  // 1. 定义着色器材质 ShaderMaterial
  // 2. 设置顶点着色器和片元着色器代码
  const geometry = new THREE.PlaneGeometry(1, 1);
  geometry.setAttribute('position2', geometry.attributes.position);
  const material = new THREE.ShaderMaterial({
   
    vertexShader: `
    attribute vec3 position2;

    uniform float posX;

      void main() {
        // gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position2, 1.0);

        vec4 v = vec4(position2, 1.0);
             v.x += posX;

        gl_Position = projectionMatrix * viewMatrix * modelMatrix * v;
      }
    `,
    // 片元着色器
    // gl_FragColor 内置变量，用于接收像素点颜色数值，值要求是 vec4 类型
    fragmentShader: `
      void main() {
        gl_FragColor = vec4(0, 1.0, 0, 1.0);
      }
    `,
    uniforms:{
      posX:{
        value:2.0 // 初始值位移2个单位
      }
    }
  });
  const mesh = new THREE.Mesh(geometry, material);
  scene.add(mesh);
}


init()
createBasic()
renderLoop() 
 

